#
# Makefile
#
# Copyright (C) 2010 Creytiv.com
#

TOPDIR = $(CURDIR)
RM = rm -f
MV = mv

CONFIGSRC = $(TOPDIR)/config
DEPENDSRC = $(CONFIGSRC)/makedepend
IMAKESRC  = $(CONFIGSRC)/imake
IMAKE     = $(IMAKESRC)/imake
IMAKE_CMD = $(IMAKE) -I$(IRULESRC) $(IMAKE_DEFINES)
MAKE_FLAGS= --no-print-directory
MAKE_OPTS = -f xmakefile $(MAKE_FLAGS)
MAKE_CMD  = $(MAKE) $(MAKE_OPTS)
FLAGS = $(MFLAGS) -f makefile.ini BOOTSTRAPCFLAGS="$(BOOTSTRAPCFLAGS)" CC="$(CC)"

CCCOLOR   = "\033[34m"
SRCCOLOR  = "\033[33m"
ENDCOLOR  = "\033[0m"

ifndef V
QUIET_CC = @printf '    %b %b\n' $(CCCOLOR)CC$(ENDCOLOR) $(SRCCOLOR)$@$(ENDCOLOR) 1>&2;
endif

.PHONY:

default: all

all:
	@$(MAKE_CMD) xmakefile-exists || $(MAKE) --no-print-directory all-initial

all-initial:
	@echo ""
	@echo Please use make World, or on NT use nmake World.Win32.
	@echo ""

World:
	@if [ -f xmakefile ]; then \
		$(MAKE_CMD) -k distclean || \
		$(MAKE_CMD) -k clean || \
		$(RM) xmakefile; \
	fi
	@cd $(IMAKESRC) && $(MAKE) $(FLAGS) clean
	@$(MAKE) $(MFLAGS) Makefile.boot

.PRECIOUS: Makefile

# This is just a sequence of bootstrapping steps we have to do.
# The steps are listed as separate targets so clearmake can wink in
# the Makefile.proto files.

Makefile.boot: imake.bootstrap

imake.proto:
	@cd $(IMAKESRC) && $(MAKE) $(FLAGS)

$(DEPENDSRC)/Makefile.proto: imake.proto

depend.bootstrap: $(DEPENDSRC)/Makefile.proto

$(IMAKESRC)/Makefile.proto: depend.bootstrap

imake.bootstrap: $(IMAKESRC)/Makefile.proto
	@$(MAKE) $(MFLAGS) $(MAKE_FLAGS) xmakefile

xmakefile: Imakefile
	@$(RM) xmakefile
	$(IMAKE_CMD) -s xmakefile -DTOPDIR=$(TOPDIR) -DCURDIR=$(CURDIR)
